package com.capgemini.cn.authority.core.security;

import org.pac4j.core.context.Pac4jConstants;
import org.pac4j.core.context.WebContext;
import org.pac4j.core.credentials.UsernamePasswordCredentials;
import org.pac4j.core.exception.HttpAction;
import org.pac4j.core.profile.creator.ProfileCreator;
import org.springframework.stereotype.Component;

/**
 * Created at 2017/9/6
 *
 * @author Andriy
 */
@Component(value = SecurityProfileCreator.COMPONENT_NAME)
public class SecurityProfileCreator implements ProfileCreator<UsernamePasswordCredentials, SecurityProfile> {

    public static final String COMPONENT_NAME = "securityProfileCreator";

    @Override
    public SecurityProfile create(UsernamePasswordCredentials credentials, WebContext context) throws HttpAction {
        SecurityProfile result = new SecurityProfile();
        result.setClientName(credentials.getClientName());
        result.setId(credentials.getUsername());
        result.addAttribute(Pac4jConstants.USERNAME, credentials.getUsername());
        return result;
    }
}
